package com.umeox.ota.mvvm.viewModel;

import android.app.Application;

import androidx.databinding.ObservableField;
import androidx.databinding.ObservableInt;

import com.abupdate.fotainject.FotaInject;
import com.abupdate.fotainject.FotaInjector;
import com.abupdate.iot_libs.data.local.DeviceInfo;
import com.abupdate.iot_libs.data.remote.VersionInfo;
import com.abupdate.iot_libs.engine.otaStatus.OtaStatus;
import com.abupdate.iot_libs.engine.policy.PolicyManager;
import com.abupdate.sdk_annotation.Inject;
import com.abupdate.trace.Trace;
import com.umeox.ota.R;
import com.umeox.ota.mvvm.BaseViewModel;
import com.umeox.ota.mvvm.model.MainModel;

/**
 * @author fighter_lee
 * @date 2018/7/19
 */
public class PickFileViewModel extends BaseViewModel<MainModel> {
    private static final String TAG = "PickFileViewModel";

    private FotaInject fotaInject;

    public final ObservableField<String> versionName = new ObservableField<>();

    public final ObservableField<String> progressObservable = new ObservableField<>();
    public final ObservableInt dlProgress = new ObservableInt();

    public final ObservableField<String> leftTipsTitle = new ObservableField<>();

    public final ObservableField<String> leftTipsContent = new ObservableField<>();

    public final ObservableField<OtaStatus> otaStatusObservableField = new ObservableField<>();

    @Inject
    PolicyManager policyManager;

    @Inject
    VersionInfo versionInfo;

    @Inject
    DeviceInfo deviceInfo;

    public PickFileViewModel(Application context, MainModel model) {
        super(context, model);
        fotaInject = FotaInjector.inject(this);
        initData();
    }

    public void initData() {
        OtaStatus state = mModel.getState();
        Trace.d(TAG, "current state: " + state.name());
        if (state == null) {
            Trace.d(TAG, "state == null ");
            otaStatusObservableField.set(OtaStatus.IDLE);
        }
        if (state == OtaStatus.IDLE || state == OtaStatus.CHECKING) {
            versionName.set(String.format(getApplication().getResources().getString(R.string.current_version), deviceInfo.version));
            leftTipsTitle.set(getApplication().getResources().getString(R.string.tips));
        } else {
            if (versionInfo != null) {
                versionName.set(String.format(getApplication().getResources().getString(R.string.new_version), versionInfo.versionName));
                leftTipsContent.set(getApplication().getResources().getString(R.string.new_version_tips) + "\n" + versionInfo.getReleaseNoteByCurrentLanguage());
            }else {
                Trace.d(TAG, "versionInfo is null ");
            }
            leftTipsTitle.set(getApplication().getResources().getString(R.string.release_note_text));
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        fotaInject.unInject();
    }
}
